// pages/banghui/fensijiangli.js

const app = getApp();
var util = require('../../utils/util.js')
var http = require('../../utils/server.js')
Page({
  data: {
    id:null,
    bangzhu:true,
    showDialog:false,
    productList:[],
    page:1,
    noMore:false,
    user:null,
    showSetting:false,
  },

  onLoad: function (options) {
    var that = this;
    that.data.user = wx.getStorageSync('userInfo');
    console.info(that.data.user)
    that.data.id = options.gang_id;
    that.loadList();
    if (that.data.id == that.data.user.gangId && that.data.user.gangRole == 5){
      that.setData({ showSetting: true })
    }
  },
  loadList:function(){
    var that = this;
    var page = that.data.page;
    http.post({
      url: "Gang/getFansReward",
      gang_id: that.data.id,
      page: page,
      limit:10,
      type: ''
    }, function (data) {
      console.log('帮会信息', data);
      if(page == 1){
        wx.stopPullDownRefresh();
      }
      that.setData({ productList: data, noMore: data.length < 10 });
    })
  },

  setting:function(e){
    if(!this.data.showSetting){
      return;
    } 
    var that = this;
    var currentProduct = that.data.productList[e.currentTarget.dataset.index];
    for (var i in currentProduct.specifications) {
      currentProduct.specifications[i].calc_fans_reward = (currentProduct.specifications[i].supply_price / 100).toFixed(2)
    }  
    this.setData({ showDialog: true, currentProduct: currentProduct});
  },
  postSetting:function(e){
    console.info(e.detail.value);
    var that = this;
    var data = [];
    for (var key in e.detail.value){
      var item = {};
      item.product_id = that.data.currentProduct.id,
      item.specifications = key;
      item.fans_reward = e.detail.value[key];
      data.push(item);
    }
    console.info(data);
    http.post({
      url: "Gang/setFansReward",
      data: data,
      type: ''
    }, function (data) {
      that.onPullDownRefresh();
      that.setData({ showDialog: false});
    })
  },

  onPullDownRefresh: function () {
    console.info("onPullDownRefresh")
    this.data.noMore = false;
    this.loadList();
  },

  onReachBottom: function () {
    console.info("onReachBottom")
    if(this.data.noMore){
      return;
    }
    this.data.page = this.data.page+1;
    this.loadList();
  },
  onShareAppMessage: function () {
  
  },help:function(){
    
  }  ,toggleDialog: function () {
    this.setData({
      showDialog: !this.data.showDialog,
    })
  },
})